<?php
/**
 * Created by PhpStorm.
 * User: 王崇全
 * Date: 2018/1/15
 * Time: 14:42
 */

namespace app\common\exception;

use Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;

class Index extends Handle
{
    public function render(Exception $e)
    {
        // 请求异常
        if (mb_strpos($e->getMessage(), "not exists:") !== false) {
            rj(1, "请求地址不存在");
        }

        // 参数验证错误
        if ($e instanceof ValidateException) {
            return json($e->getError(), 422);
        }

        // 请求异常
        if ($e instanceof HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }

        //api模块返回json数据
        $msg = config("app_debug") ? $e->getMessage() : "程序错误，请联系网站管理员";

        $errInfo = config("app_debug") ? [
            "file"  => $e->getFile(),
            "line"  => $e->getLine(),
            "trace" => $e->getTrace(),
        ] : [];

        rj(1, $msg, $errInfo);

        // 其他错误交给系统处理
        return parent::render($e);
    }

}